<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.is.model.*" %>
<%@ page import="com.is.control.*" %>
<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
<%
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	String passwd = request.getParameter("password");
	String isCk = request.getParameter("cookie");
	System.out.print(isCk);
	User u = UserControl.loginCheck(username);
	StringBuffer sbu = new StringBuffer();
	sbu.append("<login>");
	if(u == null){
		sbu.append("<status>用户名不存在</status>");
	} else{
		if(!u.getPassword().equals(passwd)){
			sbu.append("<status>密码错误</status>");
		} else if(u.getPassword().equals(passwd)){
			sbu.append("<status>登录成功，正在打开新页面</status>");
			int uid = u.getId();
			session.setAttribute("user",u);
			session.setAttribute("role",u.getRole());
			session.setAttribute("uid",uid);
			if(isCk!=null&&isCk.equals("1")){
			
			    Cookie cookieUID = new Cookie("CK_UID",String.valueOf(uid)); //cookie名字与servlet获取时一直
				cookieUID.setMaxAge(30*24*3600);
				cookieUID.setPath("/InterStu");
				response.addCookie(cookieUID);
				Cookie cookiePWD = new Cookie("CK_PWD",passwd); //cookie名字与servlet获取时一直
				cookiePWD.setMaxAge(30*24*3600);
				cookiePWD.setPath("/InterStu");
				response.addCookie(cookiePWD);
			}
			if(u.getRole().equals("1") || u.getRole() == "1"){
				sbu.append("<role>管理员</role>");
			} else if(u.getRole().equals("0") || u.getRole() == "0"){
				sbu.append("<role>普通用户</role>");
			} else{
				sbu.append("<role>非法账号</role>");
			}
			sbu.append("<uid>" + uid + "</uid>");
		}	
	}
	sbu.append("</login>");
	response.setContentType("text/xml; charset=UTF-8");
	out.write(sbu.toString());
%>